import {ADD, REQUEST_BEGIN, REQUEST_SUCCESS, REQUEST_FAIL} from './constants'

export const add = () => {
  return {type: ADD, val: 2}
}

export const request = () => {

  return dispatch => {
    // 请求前, isLoading --> true
    dispatch({type: REQUEST_BEGIN, isLoading: true})

    fetch("http://localhost:4000/api/foodtype")
      .then(res => res.json())
      .then(res => {
        // 请求成功
        return dispatch({type: REQUEST_SUCCESS, data: res})
      })
      .catch(err => {
        // 请求失败
        return dispatch({type: REQUEST_FAIL, err: [1, err.message]})
      })
  }
}
